﻿using System;
using System.Web.UI;
using Business;

public partial class Controls_BackEnd_Profile : UserControl
{
    protected Users User
    {
        get
        {
            var users = (Users)Session[SessionManager.SessionData] ?? new Users();
            users.Email = txtEmail.Text;
            users.FullName = txtFullName.Text;
            users.Title = txtStatus.Text;
            users.Address = txtAddress.Text;
            users.Moblie = txtMobile.Text;
            users.Avarta = GetUrlImage();
            return users;
        }
        set
        {
            Session[SessionManager.SessionData] = value;
            txtEmail.Text = value.Email;
            txtFullName.Text = value.FullName;
            txtStatus.Text = value.Title;
            txtAddress.Text = value.Address;
            txtMobile.Text = value.Moblie;
            if (string.IsNullOrEmpty(value.Avarta)) return;
            txtImage.Text = Config.ImageUrl + value.Avarta;
            ImgAvarta.ImageUrl = Config.ImageUrl + value.Avarta;

        }
    }

    private string GetUrlImage()
    {
        try
        {
            var url = txtImage.Text.ToLower();
            var link = Config.ImageUrl.ToLower();
            return url.Replace(link, string.Empty);

        }
        catch { }
        return string.Empty;
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        GetData();
    }




    /// <summary>
    /// 
    /// </summary>
    private void GetData()
    {
        Session[SessionManager.SessionControl] = "~/Controls/Common/FileManager.ascx";
        User = Config.UserId > 0 ? new Users { UserID = Config.UserId }.Users_GetByUserID() : new Users();


    }




    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AllCommand(object sender, EventArgs e)
    {
        switch (((Control)sender).ID)
        {
            case "btnSave":
                var error = User.Save();
                if (error > 0)
                {
                    User.UserID = Config.Id = error;
                    Config.Message(Page, Resources.Resource.SaveSuccess, Resources.Resource.Messenger);
                    return;
                }
                switch (error)
                {
                    case -1:
                        Config.Message(Page, string.Format(Resources.Resource.ObjectExisted, Resources.Resource.User), Resources.Resource.Warning);
                        break;
                    default:
                        Config.Message(Page, Resources.Resource.ErrorMsg, Resources.Resource.Warning);
                        break;
                }
                break;

        }

    }
}
